package top.drinyeit.weilong.fragment;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.base.LazyFragment;
import top.drinyeit.weilong.presenter.GoodsSearchResultPresenter;

/**
 * Created by bigHead on 2017/11/16.
 */

public class GoodsSearchResultFragment extends LazyFragment {

    String searchKey;
    public View include;
    public RecyclerView mRecyclerView;
    private GoodsSearchResultPresenter goodsSearchResultPresenter;
    private TextView price, sold;
    private RelativeLayout price_sort, sold_sort;
    private int priceNo = 0;
    private int soldNo = 0;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;
    public int pageIndex;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_category;
    }

    @Override
    protected void initViews() {
        include = mRootView.findViewById(R.id.include);
        mRecyclerView = mRootView.findViewById(R.id.mRecyclerView);
        price = mRootView.findViewById(R.id.price);
        sold = mRootView.findViewById(R.id.sold);
        price_sort = mRootView.findViewById(R.id.price_sort);
        sold_sort = mRootView.findViewById(R.id.sold_sort);
        shSwipeRefreshLayout = mRootView.findViewById(R.id.srl_category_result);

        include.setVisibility(View.GONE);
        initListener();
    }

    @Override
    protected void lazyLoad() {
        searchKey = getActivity().getIntent().getExtras().getString("search_key");
        goodsSearchResultPresenter = new GoodsSearchResultPresenter(this);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_goods");
        map.put("search_key", searchKey);
        map.put("page_index", 1);
        goodsSearchResultPresenter.categoryGoodsSearch(map);
    }

    private void initListener() {
        shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                Map<String, Object> map = new HashMap<>();
                map.put("action", "commodity_category_goods");
                map.put("search_key", searchKey);
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", 1);
                goodsSearchResultPresenter.categoryGoodsSearch(map);
            }

            @Override
            public void onLoading() {
                Map<String, Object> map = new HashMap<>();
                map.put("action", "commodity_category_goods");
                map.put("search_key", searchKey);
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", pageIndex + 1);
                goodsSearchResultPresenter.categoryGoodsSearch(map);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
        price_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                priceSort();
            }
        });

        sold_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soldSort();
            }
        });
    }

    private void priceSort() {
        sold.setText("销量");
        sold.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_black_51));
        sold.setCompoundDrawables(null, null, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_goods");
        map.put("search_key", searchKey);
        Drawable drawable0 = ContextCompat.getDrawable(getActivity(), R.drawable.up_arrow_icon);
        Drawable drawable1 = ContextCompat.getDrawable(getActivity(), R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (priceNo == 0) {
            map.put("price_type", 1);
            map.put("page_index", 1);
            goodsSearchResultPresenter.categoryGoodsSearch(map);
            price.setTextColor(ContextCompat.getColor(getActivity(), R.color.app_green));
            price.setCompoundDrawables(null, null, drawable0, null);
            price.setText("价低优先");
            priceNo = 1;
            soldNo = 0;
        } else if (priceNo == 1) {
            map.put("price_type", 2);
            map.put("page_index", 1);
            goodsSearchResultPresenter.categoryGoodsSearch(map);
            price.setCompoundDrawables(null, null, drawable1, null);
            price.setText("价高优先");
            priceNo = 2;
        } else {
            map.put("price_type", 1);
            map.put("page_index", 1);
            goodsSearchResultPresenter.categoryGoodsSearch(map);
            price.setCompoundDrawables(null, null, drawable0, null);
            price.setText("价低优先");
            priceNo = 1;
        }
    }

    private void soldSort() {
        price.setText("价格");
        price.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_black_51));
        price.setCompoundDrawables(null, null, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_goods");
        map.put("search_key", searchKey);
        Drawable drawable0 = ContextCompat.getDrawable(getActivity(), R.drawable.up_arrow_icon);
        Drawable drawable1 = ContextCompat.getDrawable(getActivity(), R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (soldNo == 0) {
            map.put("sell_type", 1);
            map.put("page_index", 1);
            goodsSearchResultPresenter.categoryGoodsSearch(map);
            sold.setTextColor(ContextCompat.getColor(getActivity(), R.color.app_green));
            sold.setCompoundDrawables(null, null, drawable0, null);
            sold.setText("销量优先");
            soldNo = 1;
            priceNo = 0;
        } else if (soldNo == 1) {
            map.put("sell_type", 2);
            map.put("page_index", 1);
            goodsSearchResultPresenter.categoryGoodsSearch(map);
            sold.setCompoundDrawables(null, null, drawable1, null);
            sold.setText("销量最低");
            soldNo = 2;
        } else {
            map.put("sell_type", 1);
            map.put("page_index", 1);
            goodsSearchResultPresenter.categoryGoodsSearch(map);
            sold.setCompoundDrawables(null, null, drawable0, null);
            sold.setText("销量优先");
            soldNo = 1;
        }
    }

}
